package com.wesh.gateway.filters;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;

import java.util.Objects;

@Component
public class CheckUserInfoGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {

    private final Logger logger = LoggerFactory.getLogger(CheckUserInfoGatewayFilterFactory.class);

    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return (exchange, chain) -> {
            logger.info("检查用户信息 Filter {},{}", config.getName(), config.getValue());
            MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
            if(Objects.nonNull(queryParams) && queryParams.size() > 0){
                if(queryParams.get(config.getName()).size() == 0){
                    return null;
                }
            }
            return chain.filter(exchange);
        };
    }
}
